package com.parktech.sun.domain.model;

import com.parktech.sun.utils.HTMLHelper;
import org.xml.sax.SAXException;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: Think
 * Date: 11-1-15
 * Time: 下午11:29
 */
//@MappedSuperclass
@Entity
@DiscriminatorColumn(name="contentType")
@Table
public class ContentEntity extends BaseEntity {

    private String title;

    private String summary;

    private String content;

    private String publishDate = df.format(new Date());

    private Integer publishStatus;

    private Integer accessPermission = 0;

    private Integer orderCode = -1;

    private String location = "contentEntity";

    public ContentEntity() {
    }

    public ContentEntity(Long id) {
        super(id);
    }

    public ContentEntity(Long id, String title, String content, String location) {
        super(id);
        this.title = title;
        this.content = content;
        this.location = location;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Size(max = 120)
    @Column(length = 1000)
    public String getSummary() {
        if (summary == null || summary.equals("")) {
            String contentText = getContentText();
            if (contentText != null) {
                return contentText.substring(0, contentText.length() >= 200 ? 200 : contentText.length());
            }
        }
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(length = 2147483647)
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    public Integer getPublishStatus() {
        return publishStatus;
    }

    public void setPublishStatus(Integer publishStatus) {
        this.publishStatus = publishStatus;
    }

    public Integer getOrderCode() {
        return orderCode;
    }

    public void setOrderCode(Integer orderCode) {
        this.orderCode = orderCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ContentEntity)) return false;
        return this.id.equals(((ContentEntity)obj).getId());
    }
    @Transient
    public String getLocation() {
        return location;
    }

    public Integer getAccessPermission() {
        return accessPermission;
    }

    public void setAccessPermission(Integer accessPermission) {
        this.accessPermission = accessPermission;
    }

    @Transient
    public String getContentText() {
        if (content == null) return "";
        try {
            return HTMLHelper.getContentText(content);
        } catch (IOException e) {
            e.printStackTrace();
            return content;
        } catch (SAXException e) {
            e.printStackTrace();
            return content;
        }
    }
}
